package com.qf.gh.utils;



import com.alibaba.druid.pool.DruidDataSource;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class DateSourceUtils {
    private static DruidDataSource dr = new DruidDataSource();
    static {
        Properties pp = new Properties();
        InputStream inputStream = DateSourceUtils.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            pp.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("加载db.properties文件失败!!");
        }
        dr.setUrl(pp.getProperty("druid.url"));
        dr.setUsername(pp.getProperty("druid.username"));
        dr.setPassword(pp.getProperty("druid.password"));
        dr.setDriverClassName(pp.getProperty("druid.driverClassName"));
    }
    public static DataSource getDataSource() {
        return dr;
    }

    public static Connection getConnection() {
        try {
            return dr.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("获取连接对象失败");
            return null;
        }
    }

    public static void main(String[] args) {
        System.out.println(getConnection());
    }
}
